/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ChateoController;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

/**
 *
 * @author adrian
 * @author olga
 */
public class ChatMessage {

    private String text;
    private User sender;
    private User receiver;
    private boolean privato;
    private GregorianCalendar date;
    private static int nbrMessage = 0;
    private int id;

    /**
     *
     * @param text is the text message to be sent or received
     * @param sender is the user sending the text message
     * @param receiver is the destination user
     * @param privato is to tell if the message is private or not
     */
    public ChatMessage(String text, User sender, User receiver, boolean privato) {
        this.date = new GregorianCalendar();
        this.text = text;
        this.sender = sender;
        this.receiver = receiver;
        this.privato = privato;
        nbrMessage++;
        this.id = nbrMessage;

    }

    /**
     *
     * @return the text that was specified
     */
    public String getText() {
        return text;
    }

    /**
     *
     * @param text is the text that a user wants to send
     */
    public void setText(String text) {
        this.text = text;
    }

    /**
     *
     * @return the user sending the message
     */
    public User getSender() {
        return sender;
    }

    /**
     *
     * @param sender is the user sending the message
     */
    public void setSender(User sender) {
        this.sender = sender;
    }

    /**
     *
     * @return the user receiving the message
     */
    public User getReceiver() {
        return receiver;
    }

    /**
     *
     * @param receiver is the user receiving the message
     */
    public void setReceiver(User receiver) {
        this.receiver = receiver;
    }

    /**
     *
     * @return the text message emission/reception date
     */
    public GregorianCalendar getDate() {
        return date;
    }

    /**
     *
     * @param date is the text message emission/reception date
     */
    public void setDate(GregorianCalendar date) {
        this.date = date;
    }

    /**
     *
     * @return the total number of text messages sent/received
     */
    public static int getNbrMessage() {
        return nbrMessage;
    }

    /**
     *
     * @param nbrMessage is the total number of text messages sent/received
     */
    public static void setNbrMessage(int nbrMessage) {
        ChatMessage.nbrMessage = nbrMessage;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    /**
     *
     * @return is yes or no the message is private
     */
    public String privacy() {
        if (this.privato) {
            return "[Private]";
        } else {
            return "[Public]";
        }
    }

    /**
     *
     * @return a string representation of the object
     */
    @Override
    public String toString() {
        String out = format(date) + "\n";
        out += "From : " + sender.getLogin() + " \n";
        out += "To : " + receiver.getLogin() + " \n";
        out += "Message : " + text + "\n";
        return out;
    }

    /**
     *
     * @param calendar is the default GregorianCalendar using the current time
     * in the default time zone with the default locale
     * @return the format needed
     */
    public static String format(GregorianCalendar calendar) {
        SimpleDateFormat fmt = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
        fmt.setCalendar(calendar);
        String dateFormatted = fmt.format(calendar.getTime());
        return dateFormatted;
    }
}
